home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapter5 / execute.c < prev    next >
C/C++ Source or Header  |  1996-04-27  |  9KB  |  265 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "execute.h"
  5. #include "sqlext.h"  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "SQLExecute()"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  109. {
  110. static HENV  hEnv  = NULL;
  111. static HDBC  hDBC  = NULL;
  112. static HWND  hList = NULL;
  113. static UCHAR szSqlStr[] = 
  114.    "Insert into department( dept_id, dept_name, dept_head_id ) "
  115.    "Values ('600', 'Accounting', '750' )";
  116.  
  117.    switch( uMsg )
  118.    {
  119.       case WM_CREATE :
  120.               {
  121.                  RETCODE retCode;
  122.  
  123.                  // Allocate Environment and Connection.
  124.                  //.....................................
  125.                  SQLAllocEnv( &hEnv );
  126.                  SQLAllocConnect( hEnv, &hDBC );
  127.  
  128.                  // Connect to the data source
  129.                  //...........................
  130.                  retCode = SQLConnect( hDBC, 
  131.                              "Test Data Source", SQL_NTS,
  132.                              "DBA",              SQL_NTS,  
  133.                              "SQL",              SQL_NTS );
  134.  
  135.                  if ( retCode == SQL_SUCCESS )
  136.                  {
  137.                     int nTabSize = 60;
  138.  
  139.                     hList = CreateWindow( "LISTBOX", "",    
  140.                                LBS_NOTIFY | LBS_USETABSTOPS |
  141.                                LBS_NOINTEGRALHEIGHT  | 
  142.                                WS_BORDER  | WS_CHILD | 
  143.                                WS_VISIBLE | WS_VSCROLL, 
  144.                                0, 0, 0, 0, 
  145.                                hWnd, (HMENU)101,
  146.                                hInst, NULL );
  147.                     
  148.                     SendMessage( hList, LB_SETTABSTOPS, 
  149.                                  1, (LPARAM)&nTabSize );
  150.                  }
  151.               }
  152.               break;
  153.  
  154.       case WM_SIZE :
  155.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  156.                                        HIWORD( lParam ), TRUE );
  157.               break; 
  158.               
  159.       case WM_COMMAND :
  160.               switch( LOWORD( wParam ) )
  161.               {
  162.                  case IDM_TEST :
  163.                         {
  164.                            HSTMT   hStmt;
  165.                            RETCODE retCode;
  166.  
  167.                            // Allocate a statement handle for the query.
  168.                            // ..........................................
  169.                            SQLAllocStmt( hDBC, &hStmt );
  170.  
  171.                            // Call SQLPrepare() to compile the query.
  172.                            // .......................................
  173.                            retCode = SQLPrepare( hStmt, szSqlStr, 
  174.                                                  sizeof(szSqlStr) );
  175.  
  176.                            // Call SQLExecute() to execute the query.
  177.                            // .......................................
  178.                            if( retCode == SQL_SUCCESS || 
  179.                                retCode == SQL_SUCCESS_WITH_INFO )
  180.                            {
  181.                               retCode = SQLExecute( hStmt );
  182.  
  183.                               if( retCode == SQL_SUCCESS || 
  184.                                   retCode == SQL_SUCCESS_WITH_INFO )
  185.                               {
  186.                                  // Commit the transaction.
  187.                                  // .......................
  188.                                  SQLTransact( SQL_NULL_HENV, hDBC, 
  189.                                               SQL_COMMIT );
  190.  
  191.                                  SendMessage( hList, LB_ADDSTRING, 
  192.                                               0, (LPARAM)"Insert successful." );
  193.                               }
  194.                               else
  195.                               {
  196.                                  // Rollback the transaction.
  197.                                  // .........................
  198.                                  SQLTransact( SQL_NULL_HENV, hDBC, 
  199.                                               SQL_ROLLBACK);
  200.  
  201.                                  SendMessage( hList, LB_ADDSTRING, 
  202.                                               0, (LPARAM)"Insert failed." );
  203.                               }
  204.                            }
  205.  
  206.                            // Free the statement handle.
  207.                            // ..........................
  208.                            SQLFreeStmt( hStmt, SQL_DROP );
  209.                         }
  210.                         break;
  211.  
  212.                  case IDM_ABOUT :
  213.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  214.                         break;
  215.  
  216.                  case IDM_EXIT :
  217.                         DestroyWindow( hWnd );
  218.                         break;
  219.               }
  220.               break;
  221.       
  222.       case WM_DESTROY :
  223.               PostQuitMessage(0);
  224.  
  225.               // Disconnect Environment.
  226.               //........................
  227.               SQLDisconnect( hDBC );
  228.  
  229.               // Free Connection and Environment.
  230.               //.................................
  231.               SQLFreeConnect( hDBC );
  232.               SQLFreeEnv( hEnv );
  233.               break;
  234.  
  235.       default :
  236.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  237.    }
  238.  
  239.    return( 0L );
  240. }
  241.  
  242.  
  243. LRESULT CALLBACK About( HWND hDlg,           
  244.                         UINT message,        
  245.                         WPARAM wParam,       
  246.                         LPARAM lParam)
  247. {
  248.    switch (message) 
  249.    {
  250.        case WM_INITDIALOG: 
  251.                return (TRUE);
  252.  
  253.        case WM_COMMAND:                              
  254.                if (   LOWORD(wParam) == IDOK         
  255.                    || LOWORD(wParam) == IDCANCEL)    
  256.                {
  257.                        EndDialog(hDlg, TRUE);        
  258.                        return (TRUE);
  259.                }
  260.                break;
  261.    }
  262.  
  263.    return (FALSE); 
  264. }
  265.